package tool.library.widget;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by Mad on 2015/7/3.
 */
public class AutoCycleViewPager extends ViewPager{
    //private int pageNum;
    private int mDuration=5000;
    private Handler mHandler=new Handler();
    public AutoCycleViewPager(Context context) {
        super(context);
        addOnPageChangeListener(mListener);
    }

    public AutoCycleViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        addOnPageChangeListener(mListener);
    }
    private OnPageChangeListener mListener=new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            mHandler.removeCallbacks(autoRun);
            mHandler.postDelayed(autoRun,mDuration);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            mHandler.removeCallbacks(autoRun);
            if(state==ViewPager.SCROLL_STATE_IDLE) {
                mHandler.postDelayed(autoRun, mDuration);
            }
        }
    };
    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        resetRun();
    }
    private void resetRun(){
        mHandler.removeCallbacks(autoRun);
        PagerAdapter adapter=getAdapter();
        if(adapter==null)
            return;
        if(adapter instanceof CyclePagerAdapter)
        {
            CyclePagerAdapter cycleAdapter= (CyclePagerAdapter) adapter;
            final int pageNum=cycleAdapter.getSize();
            if(pageNum<=1)
                return;
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    setCurrentItem(1000 * pageNum,false);;
                }
            });
            mHandler.postDelayed(autoRun, mDuration);
        }
    }

    private Runnable autoRun=new Runnable() {
        @Override
        public void run() {
            int position=getCurrentItem();
            setCurrentItem(position+1,true);
        }
    };
    public static abstract class CyclePagerAdapter<T> extends PagerAdapter{
        private List<T> mList;
        //private Context mContext;

        public CyclePagerAdapter() {
            mList=new ArrayList<T>();
        }

        public CyclePagerAdapter(List<T> list) {
            mList=list;
            //mContext=context;
        }
        public List<T> getList() {
            return mList;
        }
        public int getSize(){
            if(mList==null)
                return 0;
            else
                return mList.size();
        }

        public void setList(List<T> list) {
            this.mList = list;
            notifyDataSetChanged();
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
//            mHandler.removeCallbacks(autoRun);
//            mHandler.postDelayed(autoRun,mDuration);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public int getCount() {
            if(mList==null)
            {
                return 0;
            }
            if(mList.size()<2)
                return mList.size();
            else
                return Integer.MAX_VALUE;
        }
    }
}
